/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanterre.annuaire.forStruts;

import com.nanterre.annuaire.bd.BDInteraction;
import com.nanterre.annuaire.papier.AnnuairePDF;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author albran
 */
public class Download extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    
    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String chercherDans = request.getParameter("critere");
        String chercherPar = request.getParameter("critereRecherhce");
        String chercherQuoi = request.getParameter("recherche");
        BDInteraction bdi = new BDInteraction();
       // en PROD
        String fileName = "/tmp/exportPdf.pdf";
        
        // en LOCAL
        //String fileName = "Z:/temp/exportPdf.pdf";
        int parentID = -1;
        
        java.util.ArrayList<Integer> liste = new java.util.ArrayList<Integer>();
        

        if (chercherDans != null && chercherPar != null && chercherQuoi.length() > 0) {

            if ("LIBELLE".equals(chercherPar)){
                if(!"UE".equals(chercherDans) && !"EC".equals(chercherDans)){
                    parentID = bdi.getIdFromLibelle(chercherDans, chercherQuoi);
                } else {
                    if ("UE".equals(chercherDans)){
                        liste = (ArrayList<Integer>) bdi.getAllParcoursByLibelleUE(chercherQuoi);
                    } if("EC".equals(chercherDans)){
                        
                        liste = (ArrayList<Integer>) bdi.getAllParcoursByLibelleEC(chercherQuoi);
                        
                    }
                }
            } if ("CODE_APOGEE".equals(chercherPar)) {
                if ("DOMAINE".equals(chercherDans)) {
                    parentID = bdi.getIdDomaineByApogee(chercherQuoi);
                }
                if ("MENTION".equals(chercherDans)) {
                    parentID = bdi.getIdMentionByApogee(chercherQuoi);
                }
                if ("DIPLOME".equals(chercherDans)) {
                    parentID = bdi.getIdDiplomeByApogee(chercherQuoi);
                }
                if ("PARCOURS".equals(chercherDans)) {
                    parentID = bdi.getIdParcoursByApogee(chercherQuoi);
                }
                if ("UE".equals(chercherDans)) {
                    liste = (ArrayList<Integer>) bdi.getAllParcoursByApogeeUE(chercherQuoi);
                }
                if ("EC".equals(chercherDans)) {
                    liste = (ArrayList<Integer>) bdi.getAllParcoursByApogeeEC(chercherQuoi);
                }
                
            }

            if (parentID != -1 || (liste != null && !liste.isEmpty())) {
              
                AnnuairePDF annuaire = new AnnuairePDF(fileName, "Annuaire de formations UPX", chercherDans, parentID, liste);

                File f = new File(fileName);

                if (f.exists() && !f.isDirectory()) {
                    FileInputStream fileInput = new FileInputStream(fileName);
                    int numOfBytes = fileInput.available();
                    response.setContentLength(numOfBytes);
                    ServletOutputStream stream = response.getOutputStream();
                    BufferedInputStream fif = new BufferedInputStream(fileInput);
                    response.setHeader("Content-Type", "application/comma-separated-values; charset=UTF-8");
                    response.setHeader("Content-Disposition", "attachement;filename=exportPdf.pdf");
                    
                    byte[] buffer = new byte[1024];
                    while (true) {
                        int bytesRead = fif.read(buffer, 0, buffer.length);
                        if (bytesRead < 0) {
                            break;
                        }
                        stream.write(buffer, 0, bytesRead);
                    }
                    stream.flush();
                    stream.close();
                    fif.close();
                    f.delete();
                }
            }
        }
        return null;

        
    }
}
